/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.sportclubmanager.web;

import cz.muni.fi.pa165.sportclubmanager.dto.UserDTO;
import cz.muni.fi.pa165.sportclubmanager.service.UsersService;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author juro
 */
@UrlBinding("/u/{$event}/{user.name}")
public class UsersActionBean implements ActionBean {
    private static final String INDEX = "/users/index.jsp";
    private static final String EDIT = "/users/edit.jsp";
    
    private ActionBeanContext context;
    
    @SpringBean
    protected UsersService usersService;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"save", "add"}, field = "name", required = true),
        @Validate(on = {"save", "add"}, field = "password", required = true),
        @Validate(on = {"save", "add"}, field = "role", required = true)
    })
    private UserDTO user;

    public UserDTO getUser() {
        return user;
    }

    public void setUser(UserDTO user) {
        this.user = user;
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadUserFromDatabase() {
        String ids = context.getRequest().getParameter("user.name");
        user = usersService.find(ids);
    }
    
    @DefaultHandler
    public Resolution index() {
        return new ForwardResolution(INDEX);
    }

    public Resolution edit() {
        return new ForwardResolution(EDIT);
    }

    public Resolution add() {
        try{
          usersService.createUser(user);        
          getContext().getMessages().add(new SimpleMessage("User added"));                                                                      
        }
        catch(Exception ex){
           getContext().getMessages().add(new SimpleMessage("error: "+ex.getLocalizedMessage()));                                                                      
        }            
        return new RedirectResolution(this.getClass(), "index");
    }

    public Resolution delete() {        
        try{
          usersService.removeUser(user.getName());        
          getContext().getMessages().add(new SimpleMessage("User removed"));                                                                      
        }
        catch(Exception ex){
           getContext().getMessages().add(new SimpleMessage("error: "+ex.getLocalizedMessage()));                                                                      
        }                    
        return new RedirectResolution(this.getClass(), "index");
    }

    public Resolution save() {
        try{
          usersService.updateUser(user);        
          getContext().getMessages().add(new SimpleMessage("User updated"));                                                                      
        }
        catch(Exception ex){
           getContext().getMessages().add(new SimpleMessage("error: "+ex.getLocalizedMessage()));                                                                      
        }                    
        return new RedirectResolution(this.getClass(), "index");
    }
    
    public List<UserDTO> getUsers() {
        return usersService.getUsers();
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}
